﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using System.Globalization;

namespace ChamCong.Admin
{
    public partial class QuanLyPhongBan : BasePage
    {
        PhongBanBLL PBBLL = new PhongBanBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpdate.Visible = false;
        }

        private void Reset()
        {
            txtMaPB.Text = null;
            txtMaPB.Enabled = true;
            txtTenPB.Text = null;
            txtTenPB.Enabled = true;
            txtMoTa.Text = null;
            txtMoTa.Enabled = true;
            txtNgayThanhLap.Text = null;
            txtNgayThanhLap.Enabled = true;

            lbthongbao.Text = null;
            lbthongbao2.Text = null;
            btnThem.Visible = true;
            btnUpdate.Visible = false;
        }

        private void ShowEditButton()
        {
            btnThem.Visible = false;
            btnUpdate.Visible = true;
            btnUpdate.Text = "Cập nhật";
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            PHONGBAN PB = new PHONGBAN();
            PB.MaPB = int.Parse(txtMaPB.Text);
            if (PB.MaPB == 0)
            {
                lbthongbao.Text = "Không đặt giá trị bằng 0 cho Mã Phòng Ban";
            }
            else
            {
                PB.TenPB = txtTenPB.Text;
                PB.MoTa = txtMoTa.Text;

                IFormatProvider format = new CultureInfo("vi-VN");

                PB.NgayThanhLap = (DateTime)Convert.ToDateTime(txtNgayThanhLap.Text, format);
                //PB.NgayThanhLap = DateTime.ParseExact(txtNgayThanhLap.Text, "dd/MM/yyyy", null);

                if (PBBLL.KiemTraTonTai(PB.MaPB, PB.TenPB) != null)
                {
                    lbthongbao.Text = PBBLL.KiemTraTonTai(PB.MaPB, PB.TenPB) + " đã tồn tại!";
                }
                else
                {
                    try
                    {
                        PBBLL.ThemPB(PB);
                        dgvDSPhongBan.DataBind();
                        Reset();
                    }
                    catch (Exception ex)
                    {
                        lbthongbao.Text = ex.ToString();
                    }
                }
            }
            
        }

        protected void libnSua_Click(object sender, EventArgs e)
        {
            int mapb = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            if (IsPostBack)
            {
                PHONGBAN pb = PBBLL.LayPhongBan(mapb);
                txtMaPB.Text = pb.MaPB.ToString();
                txtMaPB.Enabled = false;
                txtTenPB.Text = pb.TenPB;
                txtMoTa.Text = pb.MoTa;
                txtNgayThanhLap.Text = pb.NgayThanhLap.ToString();
                ShowEditButton();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            PHONGBAN PB = new PHONGBAN();
            PB.MaPB = int.Parse(txtMaPB.Text);
            PB.TenPB = txtTenPB.Text;
            PB.MoTa = txtMoTa.Text;
            PB.NgayThanhLap = DateTime.ParseExact(txtNgayThanhLap.Text, "dd/MM/yyyy", null);

            if (PBBLL.KiemTraTenPB(PB.MaPB, PB.TenPB) == false)
            {
                PBBLL.SuaPB(PB);
                dgvDSPhongBan.DataBind();
                Reset();
            }
            else
            {
                lbthongbao.Text = "Mã Phòng Ban đã được sử dụng!";
                ShowEditButton();
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }

        protected void dgvDSPhongBan_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
                ImageButton imgbtSua = (ImageButton)e.Row.FindControl("imgbtSua");
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

                if (imgbtXoa != null)
                    imgbtXoa.Attributes["MaPB"] = e.Row.Cells[0].Text;
                if (imgbtSua != null)
                    imgbtSua.Attributes["MaPB"] = e.Row.Cells[0].Text;
                if (imgbtXem != null)
                    imgbtXem.Attributes["MaPB"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            lbthongbao.Text = "hello";
            ImageButton btn = (ImageButton)sender;
            int mapb = int.Parse(btn.Attributes["MaPB"]);

            try
            {
                PBBLL.XoaPB(mapb);
                dgvDSPhongBan.DataBind();
                Reset();
            }
            catch
            {
                lbthongbao2.Text = "Không thể xóa!";
            }
        }

        protected void imgbtSua_Click(object sender, ImageClickEventArgs e)
        {
            Reset();
            ImageButton btn = (ImageButton)sender;
            int mapb = int.Parse(btn.Attributes["MaPB"]);

            if (IsPostBack)
            {
                PHONGBAN PB = PBBLL.LayPhongBan(mapb);
                txtMaPB.Text = PB.MaPB.ToString();
                txtMaPB.Enabled = false;
                txtTenPB.Text = PB.TenPB;
                txtMoTa.Text = PB.MoTa;
                //txtNgayThanhLap.Text = PB.NgayThanhLap.ToString();
                txtNgayThanhLap.Text = String.Format("{0:dd/MM/yyyy}", PB.NgayThanhLap);

                ShowEditButton();
            }
        }

        protected void imgbtXem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int mapb = int.Parse(btn.Attributes["MaPB"]);

            if (IsPostBack)
            {
                PHONGBAN PB = PBBLL.LayPhongBan(mapb);

                txtMaPB.Text = PB.MaPB.ToString();
                txtMaPB.Enabled = false;

                txtTenPB.Text = PB.TenPB;
                txtTenPB.Enabled = false;

                txtMoTa.Text = PB.MoTa;
                txtMoTa.Enabled = false;

                txtNgayThanhLap.Text = String.Format("{0:dd/MM/yyyy}", PB.NgayThanhLap);
                txtNgayThanhLap.Enabled = false;

                btnThem.Visible = false;
            }
        }

    }
}